package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.service.IGoodsTemplateService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.GoodsTemplateVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/marketGoods")
public class GoodsTemplateController extends BaseController {
    @Autowired
    private IGoodsTemplateService goodsTemplateService;

    @RequestMapping("/goodsTemplate")
    @RequiresPermissions("goodsTemplate:query")
    public String goodsTemplate() {
        return "marketGoods/goodsTemplate";
    }

    @RequestMapping("/goodsTemplateNew")
    @RequiresPermissions("goodsTemplate:insert")
    public String goodsPacketNew() {
        return "marketGoods/goodsTemplateNew";
    }

    @RequestMapping("/goodsTemplateEdit")
    @RequiresPermissions("goodsTemplate:insert")
    public String goodsTemplateEdit(Model model, String id) {
        model.addAttribute("id",id);
        return "marketGoods/goodsTemplateEdit";
    }

    @RequestMapping("/goodsTemplateDetail")
    @RequiresPermissions("goodsTemplate:query")
    public String goodsTemplateDetail(Model model, String id) {
        model.addAttribute("id",id);
        return "marketGoods/goodsTemplateDetail";
    }


    @ResponseBody
    @RequestMapping("/goodsTemplate/getList")
    public ResponseList getList(int page, int limit, String status, String searchContent) {
        Company company = getCompany();
        List<GoodsTemplateVo> list = goodsTemplateService.listVoPage(company.getId(), page,limit,Utils.genInteger(status),Utils.genLikeData(searchContent));
        return ResponseList.success(goodsTemplateService.count(company.getId(),Utils.genInteger(status),Utils.genLikeData(searchContent)),list);
    }

    @ResponseBody
    @RequiresPermissions("goodsTemplate:insert")
    @RequestMapping("/goodsTemplate/add")
    public ResponseEntity add(@RequestBody GoodsTemplateVo goodsTemplateVo){
        GoodsTemplate goodsTemplate = DataUtil.genEntityFromVo(GoodsTemplate.class, goodsTemplateVo);
        List<GoodsTemplateGoods> goodsTemplateGoodsList = goodsTemplateVo.getGoodsTemplateGoodsList();
        if (Utils.isEmpty(goodsTemplate.getName())){
            return ResponseEntity.fail("名称不能为空");
        }
        if (Utils.isEmpty(goodsTemplate.getNo())){
            return ResponseEntity.fail("编号不能为空");
        }
        if (Utils.isEmpty(goodsTemplateGoodsList)){
            return ResponseEntity.fail("您未选择任何商品");
        }

        Company company = getCompany();
        if (goodsTemplateService.checkName(goodsTemplate.getName(),company.getId())){
            return ResponseEntity.fail("名称已被使用");
        }

        if (goodsTemplateService.checkNo(goodsTemplate.getNo(),company.getId())){
            return ResponseEntity.fail("编号已被使用");
        }

        DataUtil.processEntity(company,goodsTemplate);

        Map<String,Object> map = new HashMap<>();
        map.put("goodsTemplateId",goodsTemplate.getId());
        DataUtil.processList(company,goodsTemplateGoodsList,map);

        goodsTemplateService.insert(goodsTemplate,goodsTemplateGoodsList);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("goodsTemplate:insert")
    @RequestMapping("/goodsTemplate/update")
    public ResponseEntity update(@RequestBody GoodsTemplateVo goodsTemplateVo){
        GoodsTemplate goodsTemplate = DataUtil.genEntityFromVo(GoodsTemplate.class, goodsTemplateVo);
        List<GoodsTemplateGoods> goodsTemplateGoodsList = goodsTemplateVo.getGoodsTemplateGoodsList();
        if (Utils.isEmpty(goodsTemplate.getName())){
            return ResponseEntity.fail("套餐名称不能为空");
        }
        if (Utils.isEmpty(goodsTemplate.getNo())){
            return ResponseEntity.fail("套餐编号不能为空");
        }
        if (Utils.isEmpty(goodsTemplateGoodsList)){
            return ResponseEntity.fail("您未选择任何商品");
        }

        Company company = getCompany();

        GoodsTemplate old = goodsTemplateService.getById(goodsTemplateVo.getId());
        if (!old.getName().equals(goodsTemplateVo.getName()) && goodsTemplateService.checkName(goodsTemplateVo.getName(),company.getId())){
            return ResponseEntity.fail("套餐名称已被使用");
        }

        if (!old.getNo().equals(goodsTemplateVo.getNo()) && goodsTemplateService.checkNo(goodsTemplateVo.getNo(),company.getId())){
            return ResponseEntity.fail("套餐编号已被使用");
        }

        DataUtil.processEntity(false, company,goodsTemplate);

        Map<String,Object> map = new HashMap<>();
        map.put("goodsTemplateId",goodsTemplate.getId());
        DataUtil.processList(company,goodsTemplateGoodsList,map);

        goodsTemplateService.update(goodsTemplate,goodsTemplateGoodsList);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("goodsTemplate:insert")
    @RequestMapping("/goodsTemplate/delete")
    public ResponseEntity delete(@RequestBody GoodsTemplate goodsTemplate){
        goodsTemplateService.delete(goodsTemplate);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequestMapping("/goodsTemplate/getVoById")
    public ResponseData getVoById(String id){
        GoodsTemplateVo goodsTemplateVo = goodsTemplateService.getVoById(id);
        return ResponseData.success(goodsTemplateVo);
    }

    @ResponseBody
    @RequestMapping("/goodsTemplate/getVoByIdWithStock")
    public ResponseData getVoByIdWithStock(String id,String houseId){
        if (Utils.isEmpty(id) || Utils.isEmpty(houseId)){
            return ResponseData.fail("传入参数错误");
        }
        GoodsTemplateVo goodsTemplateVo = goodsTemplateService.getVoByIdWithStock(id,houseId);
        return ResponseData.success(goodsTemplateVo);
    }
}
